package org.yray.yaml.utils;

import org.springframework.boot.system.ApplicationHome;

import java.io.File;

public class FileUtils {

    public static String getAbsoluteFileName(String filename)
    {
        String path=getPath(filename);
        if(path==null)
            return null;
        return path+File.separator+filename;

    }


    public static String getPath(String filename)
    {
        ApplicationHome h = new ApplicationHome(FileUtils.class);
        File jarF = h.getSource();
        if(jarF==null)
        {
            jarF=new File(h.getDir().getPath()+File.separator+"target"+File.separator+"classes");
        }
        File path = new File(jarF.getParentFile().toString()+File.separator+filename);
        if(path.exists())
        {
            return path.getParentFile().getPath();
        }
        path=new File(jarF.toString()+File.separator+filename);
        if(path.exists())
        {
            return path.getParentFile().getPath();
        }
        String pathStr=jarF.toString();
        if(pathStr.indexOf("jar")>-1)
        {
            path=new File( pathStr.substring(0,pathStr.lastIndexOf(File.separator))+File.separator+filename);
        }
        if(path.exists())
        {
            return path.getParentFile().getPath();
        }
        return null;

    }
}
